<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 * @property CI_DB_active_record $db
 */

class Setting_model extends CI_Model
{
    public function getOptionByName($name)
    {
        $name = trim($name);
        $this->db->select('value');
        $this->db->from('options');
        $this->db->where(array(
            'key'   =>  $name
        ));
        $rs = $this->db->get();
        if ($rs->num_rows != 0) {
            $return = $rs->result_array();
            $rs->free_result();
            return $return[0];
        }
        return false;
    }
    
    public function setOption($name, $option)
    {
        if (!$this->getOptionByName($name)) {
            $array = array(
                'key'   =>  $name,
                'value' =>  $option
            );
            return $this->db->insert('options', $array);
        } else {
            return $this->db->update('options', array('value' =>$option), array('key'=>$name));
        }
    }
}

?>